package com.infodms.dms.actions.sales.marketmanage.planmanage;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.infodms.dms.bean.AclUserBean;
import com.infodms.dms.common.Constant;
import com.infodms.dms.common.ErrorCodeConstant;
import com.infodms.dms.common.materialManager.MaterialGroupManagerDao;
import com.infodms.dms.dao.sales.marketmanage.plancommon.ActivitiesCommonDao;
import com.infodms.dms.dao.sales.marketmanage.planmanage.ActivitiesPlanManageDao;
import com.infodms.dms.dao.sales.marketmanage.planmanage.ActivitiesSummaryReportDao;
import com.infodms.dms.dao.sales.planmanage.SpaceSummaryReportDao;
import com.infodms.dms.exception.BizException;
import com.infodms.dms.po.TtCampaignPoseChkRPO;
import com.infodms.dms.util.CommonUtils;
import com.infoservice.mvc.context.ActionContext;
import com.infoservice.mvc.context.RequestWrapper;
import com.infoservice.po3.bean.PageResult;

public class ActivitiesSpaceSummaryReport {
	public Logger logger = Logger.getLogger(ActivitiesSpaceSummaryReport.class);   
	ActivitiesSummaryReportDao dao  = ActivitiesSummaryReportDao.getInstance();
	private ActionContext act = ActionContext.getContext();
	
	/*--------------------------------------------------   url常量  --------------------------------------------------*/
	private final String SPACE_SUMMARY_INIT_URL = "/jsp/sales/marketmanage/planmanage/activitiesSpaceSummaryReportSearch.jsp" ; // 跳转到区域活动总结提报查询页面
	private final String SPACE_SUMMARY_REPORT_URL = "/jsp/sales/marketmanage/planmanage/activitiesSpaceSummaryReport.jsp" ; // 跳转到区域活动总结提报页面
	
	/*--------------------------------------------------   对应action方法   --------------------------------------------------*/
	
	public void ActivitiesSpaceSummaryReportInit() {
		AclUserBean logonUser = (AclUserBean)act.getSession().get(Constant.LOGON_USER) ;
		
		try {
			List<Map<String, Object>> areaList = MaterialGroupManagerDao.getPoseIdBusiness(logonUser.getPoseId().toString());
			
			act.setOutData("areaList", areaList);
			
			act.setForword(SPACE_SUMMARY_INIT_URL) ;
		} catch (Exception e) {
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"区域活动总结提报页面初始化") ;
			logger.error(e1) ;
			act.setException(e1) ;
		}
	}
	
	/**
	 * 活动执行方案提报查询
	 */
	public void activitiesSummaryReportQuery(){
		AclUserBean logonUser = (AclUserBean)act.getSession().get(Constant.LOGON_USER);
		try {
			RequestWrapper request = act.getRequest();
			
			Long orgId = logonUser.getOrgId() ;
			Long poseId = logonUser.getPoseId() ;
			
			String areaId = CommonUtils.checkNull(request.getParamValue("areaId"));					// 活动业务范围
			String campaignNo = CommonUtils.checkNull(request.getParamValue("campaignNo"));			//方案编号
			String campaignName = CommonUtils.checkNull(request.getParamValue("campaignName"));		//方案名称
			String campaignSubject = CommonUtils.checkNull(request.getParamValue("campaignSubject"));	//活动主题
			String startDate = CommonUtils.checkNull(request.getParamValue("startDate"));				//开始时间
			String endDate = CommonUtils.checkNull(request.getParamValue("endDate"));					//结束时间
			
			TtCampaignPoseChkRPO tcpcrp = new TtCampaignPoseChkRPO();
			tcpcrp.setPoseId(poseId);			
			List list = dao.select(tcpcrp);
			if(!CommonUtils.isNullList(list)){
				tcpcrp = (TtCampaignPoseChkRPO)dao.select(tcpcrp).get(0);
			}else {
				throw new RuntimeException("您没有提报权限，请联系系统管理员！");
			}
			
			Map<String, String> summaryMap = new HashMap<String, String>() ;
			
			summaryMap.put("poseId", poseId.toString()) ;
			summaryMap.put("orgId", orgId.toString()) ;
			summaryMap.put("areaId", areaId) ;
			summaryMap.put("campaignNo", campaignNo.trim()) ;
			summaryMap.put("campaignName", campaignName.trim()) ;
			summaryMap.put("campaignSubject", campaignSubject.trim()) ;
			summaryMap.put("startDate", startDate) ;
			summaryMap.put("endDate", endDate) ;
			
			Integer curPage = request.getParamValue("curPage") != null ? Integer.parseInt(request.getParamValue("curPage")) : 1 ;
			
			PageResult<Map<String, Object>> ps = SpaceSummaryReportDao.activitiesSpacePlanReportQuery(summaryMap, curPage, Constant.PAGE_SIZE) ;
			
			act.setOutData("ps", ps);
		} catch(RuntimeException e) {
			logger.error(e) ;
			act.setException(e) ;
		} catch(Exception e) {//异常方法
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"区域活动总结提报查询");
			logger.error(logonUser,e1);
			act.setException(e1);
		}
	}
	
	/**
	 * 活动总结提报页面初始化
	 */
	public void showActivitiesSummaryReportPage(){
		AclUserBean logonUser = (AclUserBean)act.getSession().get(Constant.LOGON_USER) ;
		ActivitiesCommonDao cdao = ActivitiesCommonDao.getInstance() ;
		try {
			RequestWrapper request = act.getRequest() ;
			
			String campaignId = CommonUtils.checkNull(request.getParamValue("campaignId")) ;
			String executeId = CommonUtils.checkNull(request.getParamValue("executeId")) ;
			
			//参数MAP
			Map<String, Object> conMap=new HashMap<String, Object>();
			conMap.put("campaignId", campaignId);
			conMap.put("executeId", executeId);
			conMap.put("costId", "");
			conMap.put("targetId", "");
			
			ActivitiesPlanManageDao dddao = ActivitiesPlanManageDao.getInstance();
			String modelName = dddao.getModelName(campaignId);
			//查品牌
			Map<String, Object> map = dao.selectPinPai();
			String groupName = map.get("GROUP_NAME").toString();
			
			//方案详细信息
			Map<String, Object> planMap = SpaceSummaryReportDao.queryCampaignDetial(executeId);
			planMap.put("PROD_NAME", modelName);
			
			//附件列表
			List<Map<String, Object>> attachmentList = cdao.getAttachInfo(executeId);
			
			// 媒体投放费用
			List<Map<String, Object>> mediaList = cdao.selectMediaCost(conMap);
			// 市场活动费用
			List<Map<String, Object>> capList = cdao.selectCampaignCost(conMap);
			
			// 活动总结
			Map<String, Object> targetMap = cdao.selectCampaignTarget(conMap);
			List<Map<String, Object>> mycheckList = cdao.getMyCheckInfo(executeId,campaignId);
			
			String type = "" ;
			if(capList.size()>0 ){
				type = capList.get(0).get("COST_TYPE").toString() ;
			}
			if(mediaList.size()>0){
				type = mediaList.get(0).get("COST_TYPE").toString() ;
			}
			
			act.setOutData("type", type) ;
			
			act.setOutData("targetMap", targetMap);
			act.setOutData("planMap", planMap);
			act.setOutData("attachmentList", attachmentList);
			act.setOutData("mediaList", mediaList);
			act.setOutData("capList", capList);
			act.setOutData("groupName", groupName);
			act.setOutData("mycheckList", mycheckList);
			
			act.setForword(SPACE_SUMMARY_REPORT_URL);
		}catch(Exception e) {//异常方法
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"活动总结提报页面初始化");
			logger.error(logonUser,e1);
			act.setException(e1);
		}
	}
}
